package com.ruoyi.slope.data.weather.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.slope.data.crack.domain.SlopeCrackChangeStatistic;
import com.ruoyi.slope.data.crack.domain.SlopeCrackData;
import com.ruoyi.slope.data.weather.domain.*;
import com.ruoyi.slope.dataFile.domain.SlopeDataFile;
import com.ruoyi.slope.dataFile.service.ISlopeDataFileService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.slope.data.weather.service.ISlopeWeatherDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 气象监测数据Controller
 * 
 * @author ruoyi
 * @date 2025-05-02
 */
@RestController
@RequestMapping("/data/weather")
public class SlopeWeatherDataController extends BaseController
{    
    @Autowired
    private ISlopeDataFileService slopeDataFileService;
    @Autowired
    private ISlopeWeatherDataService slopeWeatherDataService;

    /**
     * 查询气象监测数据列表
     */
    @PreAuthorize("@ss.hasPermi('deviceInfo:deviceInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlopeWeatherData slopeWeatherData)
    {
        startPage();
        List<SlopeWeatherData> list = slopeWeatherDataService.selectSlopeWeatherDataList(slopeWeatherData);
        return getDataTable(list);
    }

    /**
     * 导出气象监测数据列表
     */
    @PreAuthorize("@ss.hasPermi('deviceInfo:deviceInfo:list')")
    @Log(title = "气象监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlopeWeatherData slopeWeatherData)
    {
        List<SlopeWeatherData> list = slopeWeatherDataService.selectSlopeWeatherDataList(slopeWeatherData);
        ExcelUtil<SlopeWeatherData> util = new ExcelUtil<SlopeWeatherData>(SlopeWeatherData.class);
        String path = util.exportExcel(response, list, "气象监测数据数据"); 
        SlopeDataFile slopeDataFile = new SlopeDataFile();
        slopeDataFile.setFileName(path);
        slopeDataFileService.insertSlopeDataFile(slopeDataFile);
    }

    /**
     * 导出气温监测数据列表
     */
    @PreAuthorize("@ss.hasPermi('deviceInfo:deviceInfo:list')")
    @Log(title = "气温监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export/temperature")
    public void exportTemperature(HttpServletResponse response, SlopeWeatherData slopeWeatherData)
    {
        List<SlopeWeatherData> list = slopeWeatherDataService.selectSlopeWeatherDataList(slopeWeatherData);

        // 计算统计数据
        double average = list.stream().mapToDouble(SlopeWeatherData::getTemperature).average().orElse(0);
        double max = list.stream().mapToDouble(SlopeWeatherData::getTemperature).max().orElse(0);
        double rms = Math.sqrt(list.stream().mapToDouble(data -> Math.pow(data.getTemperature(), 2)).average().orElse(0));

        // 创建一个新的列表，把统计数据加到列表顶部
        List<SlopeTemperatureStatistic> dataWithStatistics = new ArrayList<>();

        // 可以创建一个虚拟的数据对象用来承载统计数据
        SlopeTemperatureStatistic statisticsData = new SlopeTemperatureStatistic();
        statisticsData.setAverageTemperature(average);
        statisticsData.setAbsoluteMaxTemperature(max);
        statisticsData.setRootMeanSquareTemperature(rms);
        dataWithStatistics.add(statisticsData);  // 添加统计数据到列表

        for(SlopeWeatherData data : list){
            SlopeTemperatureStatistic statistics = new SlopeTemperatureStatistic();
            statistics.setTemperature(data.getTemperature());
            statistics.setUpdateTime(data.getUpdateTime());
            statistics.setDeviceSn(data.getDeviceSn());
            dataWithStatistics.add(statistics);
        }

        // 生成 Excel
        ExcelUtil<SlopeTemperatureStatistic> util = new ExcelUtil<>(SlopeTemperatureStatistic.class);

        // 导出带统计数据的 Excel 文件
        String path = util.exportExcel(response, dataWithStatistics, "边坡气温统计数据"); 
        SlopeDataFile slopeDataFile = new SlopeDataFile();
        slopeDataFile.setFileName(path);
        slopeDataFileService.insertSlopeDataFile(slopeDataFile);
    }

    /**
     * 导出风速监测数据列表
     */
    @PreAuthorize("@ss.hasPermi('deviceInfo:deviceInfo:list')")
    @Log(title = "气温监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export/windSpeed")
    public void exportWindSpeed(HttpServletResponse response, SlopeWeatherData slopeWeatherData)
    {
        List<SlopeWeatherData> list = slopeWeatherDataService.selectSlopeWeatherDataList(slopeWeatherData);

        // 计算统计数据
        double average = list.stream().mapToDouble(SlopeWeatherData::getWindSpeed).average().orElse(0);
        double max = list.stream().mapToDouble(SlopeWeatherData::getWindSpeed).max().orElse(0);
        double rms = Math.sqrt(list.stream().mapToDouble(data -> Math.pow(data.getWindSpeed(), 2)).average().orElse(0));

        // 创建一个新的列表，把统计数据加到列表顶部
        List<SlopeWindSpeedStatistic> dataWithStatistics = new ArrayList<>();

        // 可以创建一个虚拟的数据对象用来承载统计数据
        SlopeWindSpeedStatistic statisticsData = new SlopeWindSpeedStatistic();
        statisticsData.setAverageWindSpeed(average);
        statisticsData.setAbsoluteMaxWindSpeed(max);
        statisticsData.setRootMeanSquareWindSpeed(rms);
        dataWithStatistics.add(statisticsData);  // 添加统计数据到列表

        for(SlopeWeatherData data : list){
            SlopeWindSpeedStatistic statistics = new SlopeWindSpeedStatistic();
            statistics.setWindSpeed(data.getWindSpeed());
            statistics.setUpdateTime(data.getUpdateTime());
            statistics.setDeviceSn(data.getDeviceSn());
            dataWithStatistics.add(statistics);
        }

        // 生成 Excel
        ExcelUtil<SlopeWindSpeedStatistic> util = new ExcelUtil<>(SlopeWindSpeedStatistic.class);

        // 导出带统计数据的 Excel 文件
        String path = util.exportExcel(response, dataWithStatistics, "边坡气温统计数据"); 
        SlopeDataFile slopeDataFile = new SlopeDataFile();
        slopeDataFile.setFileName(path);
        slopeDataFileService.insertSlopeDataFile(slopeDataFile);
    }

    /**
     * 导出风向监测数据列表
     */
    @PreAuthorize("@ss.hasPermi('deviceInfo:deviceInfo:list')")
    @Log(title = "气温监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export/windDirect")
    public void exportWindDirect(HttpServletResponse response, SlopeWeatherData slopeWeatherData)
    {
        List<SlopeWeatherData> list = slopeWeatherDataService.selectSlopeWeatherDataList(slopeWeatherData);

        // 计算统计数据
        double average = list.stream().mapToDouble(SlopeWeatherData::getWindDirection).average().orElse(0);
        double max = list.stream().mapToDouble(SlopeWeatherData::getWindDirection).max().orElse(0);
        double rms = Math.sqrt(list.stream().mapToDouble(data -> Math.pow(data.getWindDirection(), 2)).average().orElse(0));

        // 创建一个新的列表，把统计数据加到列表顶部
        List<SlopeWindDirectStatistic> dataWithStatistics = new ArrayList<>();

        // 可以创建一个虚拟的数据对象用来承载统计数据
        SlopeWindDirectStatistic statisticsData = new SlopeWindDirectStatistic();
        statisticsData.setAverageWindDirection(average);
        statisticsData.setAbsoluteMaxWindDirection(max);
        statisticsData.setRootMeanSquareWindDirection(rms);
        dataWithStatistics.add(statisticsData);  // 添加统计数据到列表

        for(SlopeWeatherData data : list){
            SlopeWindDirectStatistic statistics = new SlopeWindDirectStatistic();
            statistics.setWindDirection(data.getWindDirection());
            statistics.setUpdateTime(data.getUpdateTime());
            statistics.setDeviceSn(data.getDeviceSn());
            dataWithStatistics.add(statistics);
        }

        // 生成 Excel
        ExcelUtil<SlopeWindDirectStatistic> util = new ExcelUtil<>(SlopeWindDirectStatistic.class);

        // 导出带统计数据的 Excel 文件
        String path = util.exportExcel(response, dataWithStatistics, "边坡气温统计数据"); 
        SlopeDataFile slopeDataFile = new SlopeDataFile();
        slopeDataFile.setFileName(path);
        slopeDataFileService.insertSlopeDataFile(slopeDataFile);
    }

    /**
     * 导出空气湿度监测数据列表
     */
    @PreAuthorize("@ss.hasPermi('deviceInfo:deviceInfo:list')")
    @Log(title = "气温监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export/humidity")
    public void exportHumidity(HttpServletResponse response, SlopeWeatherData slopeWeatherData)
    {
        List<SlopeWeatherData> list = slopeWeatherDataService.selectSlopeWeatherDataList(slopeWeatherData);

        // 计算统计数据
        double average = list.stream().mapToDouble(SlopeWeatherData::getHumidity).average().orElse(0);
        double max = list.stream().mapToDouble(SlopeWeatherData::getHumidity).max().orElse(0);
        double rms = Math.sqrt(list.stream().mapToDouble(data -> Math.pow(data.getHumidity(), 2)).average().orElse(0));

        // 创建一个新的列表，把统计数据加到列表顶部
        List<SlopeHumidityStatistic> dataWithStatistics = new ArrayList<>();

        // 可以创建一个虚拟的数据对象用来承载统计数据
        SlopeHumidityStatistic statisticsData = new SlopeHumidityStatistic();
        statisticsData.setAverageHumidity(average);
        statisticsData.setAbsoluteMaxHumidity(max);
        statisticsData.setRootMeanSquareHumidity(rms);
        dataWithStatistics.add(statisticsData);  // 添加统计数据到列表

        for(SlopeWeatherData data : list){
            SlopeHumidityStatistic statistics = new SlopeHumidityStatistic();
            statistics.setHumidity(data.getHumidity());
            statistics.setUpdateTime(data.getUpdateTime());
            statistics.setDeviceSn(data.getDeviceSn());
            dataWithStatistics.add(statistics);
        }

        // 生成 Excel
        ExcelUtil<SlopeHumidityStatistic> util = new ExcelUtil<>(SlopeHumidityStatistic.class);

        // 导出带统计数据的 Excel 文件
        String path = util.exportExcel(response, dataWithStatistics, "边坡气温统计数据"); 
        SlopeDataFile slopeDataFile = new SlopeDataFile();
        slopeDataFile.setFileName(path);
        slopeDataFileService.insertSlopeDataFile(slopeDataFile);
    }

    /**
     * 导出气压监测数据列表
     */
    @PreAuthorize("@ss.hasPermi('deviceInfo:deviceInfo:list')")
    @Log(title = "气温监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export/pressure")
    public void exportPressure(HttpServletResponse response, SlopeWeatherData slopeWeatherData)
    {
        List<SlopeWeatherData> list = slopeWeatherDataService.selectSlopeWeatherDataList(slopeWeatherData);

        // 计算统计数据
        double average = list.stream().mapToDouble(SlopeWeatherData::getPressure).average().orElse(0);
        double max = list.stream().mapToDouble(SlopeWeatherData::getPressure).max().orElse(0);
        double rms = Math.sqrt(list.stream().mapToDouble(data -> Math.pow(data.getPressure(), 2)).average().orElse(0));

        // 创建一个新的列表，把统计数据加到列表顶部
        List<SlopePressureStatistic> dataWithStatistics = new ArrayList<>();

        // 可以创建一个虚拟的数据对象用来承载统计数据
        SlopePressureStatistic statisticsData = new SlopePressureStatistic();
        statisticsData.setAveragePressure(average);
        statisticsData.setAbsoluteMaxPressure(max);
        statisticsData.setRootMeanSquarePressure(rms);
        dataWithStatistics.add(statisticsData);  // 添加统计数据到列表

        for(SlopeWeatherData data : list){
            SlopePressureStatistic statistics = new SlopePressureStatistic();
            statistics.setPressure(data.getPressure());
            statistics.setUpdateTime(data.getUpdateTime());
            statistics.setDeviceSn(data.getDeviceSn());
            dataWithStatistics.add(statistics);
        }

        // 生成 Excel
        ExcelUtil<SlopePressureStatistic> util = new ExcelUtil<>(SlopePressureStatistic.class);

        // 导出带统计数据的 Excel 文件
        String path = util.exportExcel(response, dataWithStatistics, "边坡气温统计数据"); 
        SlopeDataFile slopeDataFile = new SlopeDataFile();
        slopeDataFile.setFileName(path);
        slopeDataFileService.insertSlopeDataFile(slopeDataFile);
    }

}
